unit dbcComp_Trg;
// Database Comparer VCL
// Copyright (C) 1998-2013, by Boris Loboda
// www.CleverComponents.com
{$I Comparer.INC}

// Triggers compare

// 09.10.2007
// - FB21: Database triggirs support added. (BL)

interface
{ DONE 1 -oBarry : Rerlace universal trigger to simple trigger (with the same name) - AVX. }
Uses Classes,  dbcTypes, dbcClasses;

type
  // trigger types
  TBefAftType = (baUnknown, baBefore, baAfter, baResolve, baFor, baInsteadOf);
  TInsUpdDelType = (iudUnknown, iudInsert, iudUpdate, iudDelete,
                    iudInsUpd,
                    iudSelect);
  TInsUpdDelSet = Set of TInsUpdDelType;



type
  TTrgData = class(TDbObj)
  protected
    fOwnName: string;
    fOwnObj: TDbObj;
    fRestoreBody: Boolean;
    procedure SetObjCreated(aCreated: Boolean); override;
    procedure SetOwnName(aName: string);
    procedure SetRestoreBody(aValue: Boolean);
    function Ib_HeaderDef(): String;
    function Ib_Def(Body: String): String;
    function ASAHeaderDef(): String;
    function PGHeaderDef(): String;
    function PGDef(Body: String): String;
    function ASADef(Body: String): String;
    function DB2Def(Body: String): String;
    function MSSQLHeaderDef(Alter: Boolean = False): String;
    function MSSQLDef(Body: String): String;
    function OracleHeaderDef(): String;
    function DB2HeaderDef(): String;
    function OracleDef(Body: String): String;
    function MySQLDef(Body: String): String;
    function MySQLHeaderDef(): String;
    function TrgTypStr(): String;
  public
    SourceBody: String; // include AS [AS ...]
    CreateBody: String;
    Active: Boolean;
    BefAft: TBefAftType;
    InsUpdDel: TInsUpdDelSet;
    Position: integer;
    Typ: Integer; // internal IB type
    Flds: TStringList; // Sybase: update of...
    RefNew: String;    // referencing new
    RefOld: String;    // referencing old
    RefRem: String;    // referencing remote
    RefNewTbl: String;
    RefOldTbl: String;
    RefParent: String;
    WhenExpr: string;  // when (search-condition)
    ForEachRow: Boolean;
    ForEachStatement: Boolean;
    Mode: String;
    ASADialect: TASASQLDialect;
    WithAppend: Boolean;
    NotForReplication: Boolean;
    EventType: String;
    constructor Create(aObjs: TCustomObjs);
    destructor Destroy; override;
    function Def(): String; override;
    property OwnName: String read fOwnName write SetOwnName; // table/view name
    property RestoreBody: Boolean read fRestoreBody write SetRestoreBody;
    property OwnObj: TDbObj read fOwnObj; // table/view
    function SourceDef(): String; override;
    function AlterDef(): String; override;
    function EmptyBody(): String;
    function EmptyDef(): String;
    function DropDef(): String; override;
    procedure Assign(TrgData: TTrgData);
    procedure DropDependDef(); override;
    procedure DoBeforeDrop(); override;
    procedure AssignEmptyBody();
  end;

Procedure GenScriptTrigCreate(MObj, TObj: TCustomObjs; SQLExec: TCustomSQLExec; CompareObjects: TCmpObjSetType);
Procedure GenScriptTrigAlter(dbc: TCustomDBComparer);
procedure GenScriptDropTrgEmpty(MObj, TObj: TCustomObjs; SQLExec: TCustomSQLExec; CompareObjects: TCmpObjSetType);
Procedure GenScriptTrigDrop(MObj, TObj: TCustomObjs; SQLExec: TCustomSQLExec; CompareObjects: TCmpObjSetType);

type
  TTrgType = Record
               Typ: Integer;
               befAft: TBefAftType;
               InsUpdDel: TInsUpdDelSet;
               TypStr: String;
             end;
const
  TrgTypes: Array [0..13] of TTrgType =
  ((Typ:   1; BefAft: baBefore; InsUpdDel: [iudInsert]; TypStr: 'BEFORE INSERT'),
   (Typ:   2; BefAft: baAfter; InsUpdDel: [iudInsert]; TypStr: 'AFTER INSERT'),
   (Typ:   3; BefAft: baBefore; InsUpdDel: [iudUpdate]; TypStr: 'BEFORE UPDATE'),
   (Typ:   4; BefAft: baAfter; InsUpdDel: [iudUpdate]; TypStr: 'AFTER UPDATE'),
   (Typ:   5; BefAft: baBefore; InsUpdDel: [iudDelete]; TypStr: 'BEFORE DELETE'),
   (Typ:   6; BefAft: baAfter; InsUpdDel: [iudDelete];TypStr: 'AFTER DELETE'),

   (Typ:  17; BefAft: baBefore; InsUpdDel: [iudInsert, iudUpdate]; TypStr: 'BEFORE INSERT OR UPDATE'),
   (Typ:  18; BefAft: baAfter; InsUpdDel: [iudInsert, iudUpdate]; TypStr: 'AFTER INSERT OR UPDATE'),
   (Typ:  25; BefAft: baBefore; InsUpdDel: [iudInsert, iudDelete]; TypStr: 'BEFORE INSERT OR DELETE'),
   (Typ:  26; BefAft: baAfter; InsUpdDel: [iudInsert, iudDelete]; TypStr: 'AFTER INSERT OR DELETE'),
   (Typ:  27; BefAft: baBefore; InsUpdDel: [iudUpdate, iudDelete]; TypStr: 'BEFORE UPDATE OR DELETE'),
   (Typ:  28; BefAft: baAfter; InsUpdDel: [iudUpdate, iudDelete]; TypStr: 'AFTER UPDATE OR DELETE'),
   (Typ: 113; BefAft: baBefore; InsUpdDel: [iudInsert, iudUpdate, iudDelete]; TypStr: 'BEFORE INSERT OR UPDATE OR DELETE'),
   (Typ: 114; BefAft: baAfter; InsUpdDel: [iudInsert, iudUpdate, iudDelete]; TypStr: 'AFTER INSERT OR UPDATE OR DELETE')
   );

implementation

//The full source code is available on purchasing the product

end.
